package com.fanyouzhao.eshop.portal.daoMongo;

import com.fanyouzhao.eshop.core.pojo.Banner;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author: fanyouzhao
 * @date: 16:33 2021/6/30
 */
@Repository("bannerDao")
public class BannerDaoImpl implements BannerDao{

    @Autowired
    private MongoTemplate template;

    @Override
    public void insertOrUpdate(Banner banner) {
        Banner save = template.save(banner);
    }

    @Override
    public Banner findOne(Banner banner) {
        return template.findById(banner.getId(), Banner.class);
    }

    @Override
    public List<Banner> findAll(Banner banner) {
        Query query = new Query(Criteria.where("position").is(banner.getPosition()));
        return template.find(query, Banner.class, "bannerList");
    }

    @Override
    public void delete(Banner banner) {
        DeleteResult remove = template.remove(banner);
    }


}
